//
//  EmoticonVC.m
//  XinLang_OC
//
//  Created by Boss Lin on 16/5/16.
//  Copyright © 2016年 cheuhui. All rights reserved.
//

#import "EmoticonVC.h"
#import "EmoticonPackage.h"
#import "EmoticonCell.h"

#define baseTag 1000


@interface EmoticonVC ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong) UICollectionView *collectionView;

@property(nonatomic,strong) UIToolbar *toolbar;

/// 所有表情包模型
@property(nonatomic,strong) NSArray<EmoticonPackage *> *packages;

/// 记录选中的按钮
@property(nonatomic,strong) UIButton *selectedButton;


@end

@implementation EmoticonVC

- (instancetype)initWithTextView:(UITextView *)textView {
    if (self = [super init]) {
        self.textView = textView;
    }
    return self;
}

+ (instancetype)initWithTextView:(UITextView *)textView {
    return [EmoticonVC initWithTextView:textView];
}


- (void)viewDidLoad {
    [super viewDidLoad];

    
    
  
}

- (void)prepareUI {
    // 添加子控件
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.toolbar];
    
    // 添加约束
    
    self.collectionView.translatesAutoresizingMaskIntoConstraints = NO;
    self.toolbar.translatesAutoresizingMaskIntoConstraints = NO;
    
    // VFL
    // collectionView 水平方向, 左右都距离父控件0
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[cl]-0-|" options:0 metrics:nil views:@{@"cl": self.collectionView}]];
    
    // toolBar水平方向, 左右都距离父控件0
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[tb]-0-|" options:0 metrics:nil views:@{@"tb": self.toolbar}]];
    
    // 垂直方向.
    // 1.collectionView顶部和父控件顶部重合.
    // 2.collectionView底部和toolBar顶部重合
    // 3.toolBar高度44
    // 4.toolBar底部和父控件重合
    // [tb(44)] 44表示宽度还是高度,看前面的方向是H还是V, H: 宽44, V: 高度44
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[cl]-0-[tb(44)]-0-|" options:0 metrics:nil views:@{@"cl": self.collectionView, @"tb": self.toolbar}]];
    
    // 设置toolBar内容
    [self setupToolbar];
    
    // 设置collectionView
    [self setupCollectionView];
}

- (void)setupToolbar {
    NSMutableArray<UIBarButtonItem *> *items = [NSMutableArray array];
    
    // 按钮的tag
    NSInteger index = baseTag;
    
    // 获取表情包模型的名称
    for (EmoticonPackage *package in self.packages) {
        // 获取每个表情包的名称
        NSString *title = package.group_name_cn;
        
        // 创建按钮
        UIButton *button = [UIButton new];
        
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateHighlighted];
        [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateSelected];
        [button sizeToFit];
        button.tag = index;
        
        [button addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchDragInside];
        
        // 最近按钮默认高亮
        if (index == baseTag) {
            [self switchSelectedButton:button];
        }
        // 创建UIBarButtonItem
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
        // 添加item到数组
        [items addObject:item];
        // 添加弹簧
        [items addObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]];
        index ++;
    }
    // 移除最后一根弹簧
    [items removeLastObject];
    // 将items数组赋值给toolbar
    self.toolbar.items = items;
}

// toolbar按钮点击事件
- (void)itemClick:(UIButton *)button {

    NSLog(@"点击按钮：%zd",button.tag);
    
    // 切换到对应的表情包的第一页表情
    // section需要-tag的起始值
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:button.tag - baseTag];
    [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionLeft];
    
    // 让点击的按钮高亮
    [self switchSelectedButton:button];
}

/**
 切换选中的按钮,让按钮高亮
 
 - parameter button: 要选中的按钮
 */
- (void)switchSelectedButton:(UIButton *)button {
    // 让原来选中的按钮，恢复正常
    self.selectedButton.selected = NO;
    // 让传入的按钮选中
    button.selected = YES;
    // 赋值
    self.selectedButton = button;
}

- (void)setupCollectionView {
    
    self.collectionView.backgroundColor = [[UIColor alloc] initWithWhite:0.8 alpha:1];
    
    // 注册cell
    [self.collectionView registerClass:[EmoticonCell class] forCellWithReuseIdentifier:@"EmoticonCell"];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource



#pragma mark - lazy load

- (NSArray<EmoticonPackage *> *)packages {
    if (!_packages) {
        _packages = [EmoticonPackage loadPackages];
    }
    return _packages;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:nil];
    }
    return _collectionView;
}

- (UIToolbar *)toolbar {
    if (!_toolbar) {
        _toolbar = [UIToolbar new];
    }
    return _toolbar;
}

@end



@implementation EmoticonLayout

- (void)prepareLayout {
    // 根据UICollectionView的大小来设置layout的大小
    CGFloat width = self.collectionView.width / 7;
    CGFloat height = self.collectionView.height / 3;
    
    // 设置 layout的item大小
    self.itemSize = CGSizeMake(width, height);
    
    // collectionView水平滚动
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    // collectionView的cell不要间距
    self.minimumInteritemSpacing = 0;
    self.minimumLineSpacing = 0;
    
    // 取消弹簧效果
    self.collectionView.bounces = false;
    
    // 不要滚动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
    // 分页
    self.collectionView.pagingEnabled = YES;
}

@end









